package gov.pbc.nn.core.usbkey;

import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreJdbcDao;
import gov.pbc.nn.core.entity.Usbkey;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

@Component
public class UsbkeyPR {

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao hibernateDao;
	@Resource(name = PbcnnCoreJdbcDao.BEAN_ID)
	private PbcnnCoreJdbcDao jdbcDao;

	@DataProvider
	public void loadData(Page<Usbkey> page, Criteria criteria) {
		DetachedCriteria dc = this.hibernateDao.buildDetachedCriteria(criteria, Usbkey.class);
		dc.add(Restrictions.eq("invalid", false));
		this.hibernateDao.pagingQuery(page, dc);
	}

	@DataProvider
	public List<Map<String, Object>> getAllUser(Page<Map<String, Object>> page, String username) {
		List<Map<String, Object>> lists = null;
		if (StringUtils.isNotBlank(username)) {
			String sql = "select u.USERNAME_,u.CNAME_ ,d.NAME_,d.ID_  from t_user u  INNER JOIN bdf2_user_dept ud on ud.USERNAME_=u.USERNAME_ INNER JOIN  t_dept d ON ud.DEPT_ID_=d.ID_  where  u.CNAME_ like ? or d.NAME_ like ? ";
			this.jdbcDao.pagingQuery(page, sql, new Object[] { username + "%", username });
		} else {
			String sql1 = "select u.USERNAME_,u.CNAME_ ,d.NAME_,d.ID_  from t_user u  INNER JOIN bdf2_user_dept ud on ud.USERNAME_=u.USERNAME_ INNER JOIN  t_dept d ON ud.DEPT_ID_=d.ID_  ";
			this.jdbcDao.pagingQuery(page, sql1);
		}
		return lists;
	}

	@DataResolver
	@Transactional
	public void saveUsbkey(Usbkey uk) throws Exception {
		EntityState state = EntityUtils.getState(uk);
		if (state == EntityState.NEW) {
			String hql = "select count(*) from " + Usbkey.class.getName()
					+ " where serialNo=:serialNo and invalid=false";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("serialNo", uk.getSerialNo());
			if (this.hibernateDao.queryForInt(hql, params) > 0) {
				throw new Exception("已经存在该Usbkey信息！");
			}
			this.hibernateDao.save(uk);
		} else if (state == EntityState.MODIFIED) {
			this.hibernateDao.update(uk);
		}
	}

	@Expose
	@Transactional
	public void deleteUsbkey(String id) {
		this.hibernateDao.delete(id, Usbkey.class);
	}

	@Expose
	public Map<String, Object> getUserBykey(String serialNo) {
		String hql = "select new map(username as username,cname as cname)from " + Usbkey.class.getName()
				+ " where serialNo=:serialNo";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("serialNo", serialNo);
		List<Map<String, Object>> mapList = this.hibernateDao.query(hql, params);
		if (mapList != null && mapList.size() > 0) {
			return mapList.get(0);
		}
		return null;
	}

}
